---
import { getCollection } from "astro:content";
import BasicMarkdownLayout from "@/layouts/BasicMarkdownLayout/index.astro";
import ItemCollectLayout from "@/layouts/ItemCollectLayout/index.astro";
import BasicProperty from "@/components/Property/BasicProperty/index.astro";

import Config from "wiki.config";
import useIcon from "@/hooks/useIcon";
/** pre import components */
import Advancement from "@markdown/advancement/IdQuery.astro";
import Advancements from "@markdown/advancement/Multiple.astro";
import ItemIcon from "@markdown/common/ItemIcon.astro";
import BasicFormula from "@formula/index.astro";
import Formula from "@formula/IdQuery.astro";
import HiddenText from "@markdown/common/HiddenText.astro";
import HiddenTextArea from "@markdown/common/HiddenTextArea.astro";
import SubItem from "@markdown/common/SubItem.astro";
import Icon from "@markdown/common/Icon.astro";
import ItemProperty from "@/components/Property/ItemProperty/IdQuery.astro";
import ColorText from "@markdown/common/ColorText.astro";
import getCnTypeFromEnType from "@/utils/getCnTypeFromEnType";
const preImportComponents = {
  ItemIcon,
  HiddenText,
  SubItem,
  Icon,
  ItemProperty,
  BasicFormula,
  Formula,
  HiddenTextArea,
  ColorText,
  Advancement,
  Advancements,
};

const editLink = Config.issues?.type === "Other" ? Config.issues.url : "";

export async function getStaticPaths() {
  const collections = await getCollection("wiki", ({ data, slug }) => {
    return !data.draft;
  });

  return collections.map((entry) => {
    const [type, _, liver] = entry.slug.split("/");
    const id = entry.slug.split("/").pop();
    return {
      params: {
        type,
        name: id,
      },
      props: {
        entry,
        type,
        liver,
        id,
        color: entry.data.color,
        collect: entry.data.collect,
      },
    };
  });
}

const { entry, type, liver, color, collect, id } = Astro.props;
const { Content, remarkPluginFrontmatter: fm } = await entry.render();
if (typeof fm.showLiver !== "boolean") fm.showLiver = true;
let item: Record<string, any> & { name?: string } = {};

const finalID = id || fm.id;

switch (type) {
  case "items":
  case "blocks":
    if (!collect) {
      item = useIcon(finalID)!;
      break;
    }

  default:
    item = {
      name: fm.name,
    };
    break;
}
---

{
  collect ? (
    <ItemCollectLayout
      liver={color || liver}
      title={item.name || "Ciallo～(∠·ω< )⌒★"}
      showLiver={fm.showLiver && type === "items"}
      editLink={editLink}
      pagefindType={getCnTypeFromEnType(type)}
      pagefindIgnore={fm.pagefindIgnore}
    >
      <Content components={preImportComponents} />
    </ItemCollectLayout>
  ) : (
    <BasicMarkdownLayout
      title={item.name}
      showLiver={fm.showLiver && type === "items"}
      color={color || liver}
      editLink={editLink}
      pagefindType={getCnTypeFromEnType(type)}
      pagefindIgnore={fm.pagefindIgnore}
      useProperty
    >
      <Content components={preImportComponents} />
      {(() => {
        switch (type) {
          case "items":
          case "blocks":
            return (
              <ItemProperty
                id={finalID}
                property={fm.property}
                slot="property"
              />
            );
          case "enchantments":
            return null;
          default:
            return <BasicProperty fm={fm as any} slot="property" />;
        }
      })()}
    </BasicMarkdownLayout>
  )
}
